package com.topnav.mc.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topnav.mc.admin.dao.mapper.DeviceOnlineTotalMapper;
import com.topnav.mc.admin.entity.Device;
import com.topnav.mc.admin.entity.DeviceOnlineTotal;
import com.topnav.mc.admin.entity.bean.DeviceBean;
import com.topnav.mc.admin.entity.bean.DeviceOnlineTotalBean;
import com.topnav.mc.admin.entity.param.DeviceOnlineTotalParam;
import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.admin.service.IDeviceOnlineTotalService;
import com.topnav.mc.admin.service.IDeviceService;
import com.topnav.mc.base.mc.McServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: kenny
 * @Date: 2023/10/31 15:34
 */
@Service
public class DeviceOnlineTotalServiceImpl extends McServiceImpl
        <DeviceOnlineTotalMapper, DeviceOnlineTotal, DeviceOnlineTotalBean, DeviceOnlineTotalParam>
    implements IDeviceOnlineTotalService{

    @Autowired
    private DeviceOnlineTotalMapper deviceOnlineTotalMapper;
    @Autowired
    private IDeviceService deviceService;

    @Override
    public boolean beforeUpdate(DeviceOnlineTotal entity){
        entity.setUpdateTime(new Date());
        return true;
    }

    @Override
    public boolean beforeSave(DeviceOnlineTotal entity){
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return true;
    }



    /**
     * 在线统计
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public Page<DeviceOnlineTotalBean> pageInfo0(Page<DeviceOnlineTotalParam> page, Wrapper<DeviceOnlineTotalParam> queryWrapper) {
        Page<DeviceOnlineTotalBean> page1 = deviceOnlineTotalMapper.selectPage0(page,queryWrapper);

        List<Long> deviceIds = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page1.getRecords())){
            for(DeviceOnlineTotalBean bean : page1.getRecords()){
                deviceIds.add(bean.getDeviceId());
            }

            QueryWrapper<DeviceParam> queryWrapper1 = new QueryWrapper<DeviceParam>().in("d.id",deviceIds);
            List<DeviceBean> deviceBeans = deviceService.listInfo(queryWrapper1);

            //转MAP
            Map<Long, DeviceBean> mapDeviceBeans = deviceBeans.stream().collect(
                    Collectors.toMap(DeviceBean::getId, (p) -> p));

            for(DeviceOnlineTotalBean bean : page1.getRecords()){
                DeviceBean deviceBean = mapDeviceBeans.get(bean.getDeviceId());
                bean.setDeviceCode(deviceBean.getCode());
                bean.setDeviceName(deviceBean.getName());
                bean.setOrgName(deviceBean.getOrgName());
                bean.setUserName(deviceBean.getUserName());
/*                bean.setOnlineMin(bean.getOnlineSec()/60);
                bean.setOnlineStr(String.valueOf(bean.getOnlineSec()/60));*/
            }
        }
        return page1;
    }
}
